package cn.bjxy.javaks.store_ms.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * jdbc工具类，提供了获取连接，以及关闭资源方法
 * @author long
 *
 */
public class JdbcUtil {

	private final static String DRIVER = "com.mysql.jdbc.Driver";
	private final static String URL = "jdbc:mysql://localhost:3306/store_ms?useSSL=false&characterEncoding=utf-8";
	private final static String NAME = "root";
	private final static String PASSWORD = "juxiewl1314";
	
	private static Connection conn;
	
	static{     //使用静态代码块初始化
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			
		}
	}
	
	private JdbcUtil() {
		//私有化构造方法，不允许new当前类的对象，
		//所有的方法均为静态方法，直接使用类名使用当前类提供的方法
	}
	
	public static Connection getConnect() {
		try {
			conn = DriverManager.getConnection(URL, NAME, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 关闭所的资源
	 * @param con
	 * @param stat
	 * @param rs
	 */
	public static void closeAll(Connection con, Statement stat, ResultSet rs) {
		try {
			if(con!=null && !con.isClosed()) {
				con.close();
				con = null;
			}
			if(stat != null) {
				stat.close();
				stat=null;
			}
			if(rs != null) {
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
